
// 引入全部掘金markdown样式
import * as themeMap from "juejin-markdown-themes/dist"
const ToolBar = `<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path d="M6 2H2.66667C2.29848 2 2 2.29848 2 2.66667V6C2 6.36819 2.29848 6.66667 2.66667 6.66667H6C6.36819 6.66667 6.66667 6.36819 6.66667 6V2.66667C6.66667 2.29848 6.36819 2 6 2Z" stroke="#1D2129" stroke-width="1.33" stroke-linejoin="round"></path>
          <path d="M6 9.3335H2.66667C2.29848 9.3335 2 9.63197 2 10.0002V13.3335C2 13.7017 2.29848 14.0002 2.66667 14.0002H6C6.36819 14.0002 6.66667 13.7017 6.66667 13.3335V10.0002C6.66667 9.63197 6.36819 9.3335 6 9.3335Z" stroke="#1D2129" stroke-width="1.33" stroke-linejoin="round"></path>
          <path d="M13.3334 2H10C9.63185 2 9.33337 2.29848 9.33337 2.66667V6C9.33337 6.36819 9.63185 6.66667 10 6.66667H13.3334C13.7016 6.66667 14 6.36819 14 6V2.66667C14 2.29848 13.7016 2 13.3334 2Z" stroke="#1D2129" stroke-width="1.33" stroke-linejoin="round"></path>
          <path d="M13.3334 9.3335H10C9.63185 9.3335 9.33337 9.63197 9.33337 10.0002V13.3335C9.33337 13.7017 9.63185 14.0002 10 14.0002H13.3334C13.7016 14.0002 14 13.7017 14 13.3335V10.0002C14 9.63197 13.7016 9.3335 13.3334 9.3335Z" stroke="#1D2129" stroke-width="1.33" stroke-linejoin="round"></path>
          </svg>`

const themeNames = Object.keys(themeMap);
const themeList = Object.keys(themeMap).filter(key => key !== "default").map(key => ({
    title: key,
    style: "",
    rewrite: "",
    theme: key,
    highlight: themeMap[key]?.highlight,
}))

function changeTheme(theme) {
    var _a;
    let str = "";
    if (typeof theme === 'string') {
        str = theme;
    } else {
        const { title, style } = theme;
        if (themeNames.includes(title)) {
            str = (_a = themeMap[title]) === null || _a === void 0 ? void 0 : _a.style;
        }
        else {
            str = style;
        }
    }
    let markdownThemeStyleElement = document.querySelector('#markdownTheme');
    if (markdownThemeStyleElement) {
        markdownThemeStyleElement.innerHTML = str;
    } else {
        markdownThemeStyleElement = document.createElement('style');
        markdownThemeStyleElement.id = 'markdownTheme';
        markdownThemeStyleElement.innerHTML = str;
        document.head.appendChild(markdownThemeStyleElement);
    }
}

export default (options) => {
    const actionItems = (options === null || options === void 0 ? void 0 : options.themeList) || themeList;
    return {
        actions: [
            {
                title: '选择主题',
                icon: ToolBar,
                handler: Object.assign({
                    type: 'dropdown',
                    actions: actionItems.map((item) => ({
                        title: item === null || item === void 0 ? void 0 : item.title,
                        handler: {
                            type: 'action',
                            click(ctx) {
                                console.log(ctx);
                                var _a;
                                let style = item === null || item === void 0 ? void 0 : item.style;
                                if (item.rewrite) {
                                    style = `${style} \n ${item.rewrite}`;
                                    console.log(style);
                                }
                                changeTheme(item);
                                (_a = options === null || options === void 0 ? void 0 : options.cb) === null || _a === void 0 ? void 0 : _a.call(options, item);
                            },
                        },
                    }))
                })
            }
        ]
    };
}

// function setTheme(themeName) {
//     var _a;
//     if (themeNames.includes(themeName)) {
//         const style = (_a = juejin_markdown_themes_dist__WEBPACK_IMPORTED_MODULE_1__[themeName]) === null || _a === void 0 ? void 0 : _a.style;
//         let markdownThemeStyleElement = document.querySelector('#markdownTheme');
//         if (markdownThemeStyleElement) {
//             markdownThemeStyleElement.innerHTML = style;
//         }
//         else {
//             markdownThemeStyleElement = document.createElement('style');
//             markdownThemeStyleElement.id = 'markdownTheme';
//             markdownThemeStyleElement.innerHTML = style;
//             document.head.appendChild(markdownThemeStyleElement);
//         }
//     }
//     else {
//         console.warn(`${themeName} not included in the built-in theme, Please call the setCustomStyle  method to pass in a custom style`);
//     }
// }
// function setCustomStyle(style) {
//     let markdownThemeStyleElement = document.querySelector('#markdownTheme');
//     if (markdownThemeStyleElement) {
//         markdownThemeStyleElement.innerHTML = style;
//     }
//     else {
//         markdownThemeStyleElement = document.createElement('style');
//         markdownThemeStyleElement.id = 'markdownTheme';
//         markdownThemeStyleElement.innerHTML = style;
//         document.head.appendChild(markdownThemeStyleElement);
//     }
// }